//
//  HWVipGiftBagCouponsProtocol.swift
//  HWanGame
//
//  Created by 王昌健 on 2021/12/14.
//  Copyright © 2021 94Wan. All rights reserved.
//

import UIKit

class HWVipGiftBagCouponsProtocol: NSObject {
    var myArr = [[1,2,3],[1,2],[1,2,3]]
    var hotArr = [[1,2,3],[1,2],[1,2,3],[1,2,3],[1,2],[1,2,3]]
    var models:[HWCouponsModel]?
    var receiveButtonClick:((_:String)->())?
}

extension HWVipGiftBagCouponsProtocol:UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
//        if section == 0 || section == myArr.count{
//            return 105*widthScale
//        }else {
            return 61*widthScale
//        }
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
//        let header = HWVipGiftBagOnPlayHeader()
        let gameHeader = HWVipGiftBagGameHeader()
//        let searchHeader = HWVipGiftBagSearchHeader()
//        if section == 0 {
//            return header
//        } else if section == myArr.count{
//            return searchHeader
//        } else {
        gameHeader.couponsModel = models?[section]
            return gameHeader
//        }
        
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return models?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return models?[section].list.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let vipGiftBagCouponsCell = HWVipGiftBagCouponsCell.cellWithTableView(tableView: tableView)
        vipGiftBagCouponsCell.model = models?[indexPath.section].list[indexPath.row]
        vipGiftBagCouponsCell.receiveButtonClick = {
            id in
            self.receiveButtonClick?(id)
        }
        return vipGiftBagCouponsCell
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if (cell.responds(to: #selector(getter: UIView.tintColor))){
            //            if tableView == self.tableView {
            let cornerRadius: CGFloat = 4*widthScale
            cell.backgroundColor = .clear
            let layer: CAShapeLayer = CAShapeLayer()
            let path: CGMutablePath = CGMutablePath()
            
            cell.updateConstraints()
            let bounds: CGRect = cell.bounds
            bounds.insetBy(dx: 25, dy: 0)
            var addLine: Bool = false
            
            if indexPath.row == 0 && indexPath.row == ( tableView.numberOfRows(inSection: indexPath.section) - 1) {
                path.addRoundedRect(in: bounds, cornerWidth: cornerRadius, cornerHeight: cornerRadius)
                
            } else if indexPath.row == 0 {
                path.move(to: CGPoint(x: bounds.minX, y: bounds.maxY))
                path.addArc(tangent1End: CGPoint(x: bounds.minX, y: bounds.minY), tangent2End: CGPoint(x: bounds.midX, y: bounds.minY), radius: cornerRadius)
                path.addArc(tangent1End: CGPoint(x: bounds.maxX, y: bounds.minY), tangent2End: CGPoint(x: bounds.maxX, y: bounds.midY), radius: cornerRadius)
                path.addLine(to: CGPoint(x: bounds.maxX, y: bounds.maxY))
                
            } else if indexPath.row == (tableView.numberOfRows(inSection: indexPath.section) - 1) {
                path.move(to: CGPoint(x: bounds.minX, y: bounds.minY))
                path.addArc(tangent1End: CGPoint(x: bounds.minX, y: bounds.maxY), tangent2End: CGPoint(x: bounds.midX, y: bounds.maxY), radius: cornerRadius)
                path.addArc(tangent1End: CGPoint(x: bounds.maxX, y: bounds.maxY), tangent2End: CGPoint(x: bounds.maxX, y: bounds.midY), radius: cornerRadius)
                path.addLine(to: CGPoint(x: bounds.maxX, y: bounds.minY))
                
            } else {
                path.addRect(bounds)
                addLine = true
            }
            
            layer.path = path
            layer.fillColor = UIColor.colorWithHexString(hexString: "#F3F4F8").cgColor
            
//            if addLine {
//                let lineLayer: CALayer = CALayer()
//                let lineHeight: CGFloat = 1.0 / UIScreen.main.scale
//                lineLayer.frame = CGRect(x: bounds.minX + 10.0, y: bounds.size.height - lineHeight, width: bounds.size.width, height: lineHeight)
//                lineLayer.backgroundColor = tableView.separatorColor?.cgColor
//                layer.addSublayer(lineLayer)
//            }
            
            let testView: UIView = UIView(frame: bounds)
            testView.layer.insertSublayer(layer, at: 0)
            testView.backgroundColor = .clear
            cell.backgroundView = testView
            //            }
        }
    }
    
}
